package org.scheduler4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.scheduler4j.util.DateUtil;

/**
 * the year class.
 * 
 * @author Eric
 * 
 */
public class Year extends DayDuration {
	/**
	 * the year number.
	 */
	private int year;

	/**
	 * create a year with a date, get the year of this date as the year number.
	 * 
	 * @param date
	 */
	public Year(Date date) {
		this(DateUtil.getYear(date));
	}

	/**
	 * create a year with specific year number.
	 * 
	 * @param year
	 */
	public Year(int year) {
		this.year = year;
		setStartDate(new Day(DateUtil.firstDayOfYear(year)));
		setEndDate(new Day(DateUtil.lastDayOfYear(year)));
	}

	/**
	 * create a year with a day, get the year of the day as the year number.
	 * 
	 * @param day
	 */
	public Year(Day day) {
		this(day.toDate());
	}

	/**
	 * check if this is a leap year.
	 * 
	 * @return
	 */
	public boolean isLeapYear() {
		return (year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0));
	}

	/**
	 * get days in this year.
	 * 
	 * @return
	 */
	public int daysInYear() {
		return isLeapYear() ? 366 : 365;
	}

	/**
	 * get all months in this year.
	 * 
	 * @return
	 */
	public List<Month> getMonths() {
		List<Month> result = new ArrayList<Month>();
		for (int i = 0; i < 12; i++) {
			result.add(new Month(year, i + 1));
		}
		return result;
	}

	public int getYear() {
		return year;
	}

	@Override
	public int hashCode() {
		return year;
	}

	@Override
	public String toString() {
		return "Year[" + year + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Year)) {
			return false;
		}
		Year tmp = (Year) obj;
		return tmp.year == year;
	}
}
